using System;
using System.Security.Cryptography;
using System.Text;
using Castle.ActiveRecord;
using CosyTrade.Data;
using CosyTrade.Organization;
using PrimaryKeyType=Castle.ActiveRecord.PrimaryKeyType;

namespace CosyTrade.Security
{
    [AddPropertyChangedNotifyInterface, PropertyChangedNotify]
    [Serializable, ActiveRecord("tabRole")]
    public class Role : BusinessObject
    {
        private readonly Guid primaryKey = Guid.NewGuid();
        private string userName;
        private string password;
        private PowerSet powerset;
        private Company onCompany;
        private bool isSystemMaster;
        private bool isCompanyMaster;

        [PrimaryKey(PrimaryKeyType.Assigned, "ROWGUID", Access = PropertyAccess.NosetterCamelcase)]
        public override Guid PrimaryKey
        {
            get { return primaryKey; }
        }

        [Property]
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        [Property]
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        [BelongsTo("PowerGuid")]
        public PowerSet Powerset
        {
            get { return powerset; }
            set { powerset = value; }
        }

        [BelongsTo("CompanyGuid")]
        public Company OnCompany
        {
            get { return onCompany; }
            set { onCompany = value; }
        }

        [Property]
        public bool IsSystemMaster
        {
            get { return isSystemMaster; }
            set { isSystemMaster = value; }
        }

        [Property]
        public bool IsCompanyMaster
        {
            get { return isCompanyMaster; }
            set { isCompanyMaster = value; }
        }

        public static string Encode(string code)
        {
            MD5 md5Encoder = new MD5CryptoServiceProvider();
            byte[] encode = md5Encoder.ComputeHash(Encoding.UTF8.GetBytes(code));
            return BitConverter.ToString(encode);
        }
    }
}